package real.yingke;

import java.util.Stack;

public class ValidBrackets {
    public class Solution {
        /**
         * 判断是否满足条件
         * @param s string字符串 字符串s
         * @return bool布尔型
         */
        public boolean isValid (String s) {
            // write code here
            int len;
            if (s == null || (len = s.length()) == 0) return true;
            Stack<Character> stack = new Stack<>();

            for (int i = 0; i < len; i++) {
                char c = s.charAt(i);
                if (c == '{' || c == '[' || c == '(') stack.push(c);
                else {
                    char t = stack.peek();
                    if (c == '}') {
                        if (t != '{') return false;
                        else stack.pop();
                    } else if (c == ']') {
                        if (t != '[') return false;
                        else stack.pop();
                    } else {
                        if (t != '(') return false;
                        else stack.pop();
                    }
                }
            }

            return stack.isEmpty();
        }
    }
}
